<template>
  <Modal
    v-model="showEditModuleModal"
    title="编辑模块"
    :mask-closable="false"
    @on-cancel="handleEditModuleCancel"
  >
    <Form
      label-position="right"
      :label-width="80"
      ref="moduleForm"
      :model="moduleForm"
      :rules="moduleValidate"
    >
      <FormItem label="模块名称" prop="name">
        <Input clearable v-model="moduleForm.name" />
      </FormItem>
      <FormItem label="所属项目" prop="project">
        <Select v-model="moduleForm.project" clearable filterable>
          <Option
            v-for="item in projects"
            :value="item.id"
            :key="item.id"
            :label="item.name"
          />
        </Select>
      </FormItem>
      <FormItem label="备注">
        <Input
          type="textarea"
          clearable
          v-model="moduleForm.remark"
          :rows="4"
        />
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="default" @click="handleEditModuleCancel">取消</Button>
      <Button type="primary" @click="handleEditModuleSubmit('moduleForm')"
        >确定</Button
      >
    </div>
  </Modal>
</template>

<script>
import { updateModule } from "@/api/HttpAutoTestService/module";
import { getProjectNames } from "@/api/HttpAutoTestService/project";

export default {
  name: "module-edit",
  props: {
    editModule: {
      type: Boolean,
      default: false,
    },
    module: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {
    showEditModuleModal: {
      get() {
        return this.editModule;
      },
      set(val) {
        this.$emit("update:editModule", val);
        this.$emit("update:module", {});
      },
    },
  },
  watch: {
    editModule(val) {
      if (val) {
        getProjectNames()
          .then((res) => {
            this.projects = res.data;
          })
          .catch((err) => {
            this.$Modal.error({
              title: "系统异常，请联系管理员……",
              content: JSON.stringify(err),
              closable: true,
            });
          });
      }
    },
    module(val) {
      if (this.showEditModuleModal) {
        this.moduleForm = { ...val };
      }
    },
  },
  data() {
    return {
      moduleForm: {
        id: "",
        name: "",
        parent: "",
        project: "",
        remark: "",
      },
      moduleValidate: {
        name: [
          { required: true, message: "请输入模块名称", trigger: "blur" },
          {
            type: "string",
            max: 20,
            message: "模块名称最多由20个字符组成",
            trigger: "blur",
          },
        ],
        project: [
          { required: true, message: "请选择所属项目", trigger: "change" },
        ],
      },
      projects: [],
    };
  },
  methods: {
    handleEditModuleCancel() {
      this.showEditModuleModal = false;
      this.moduleForm = {
        id: "",
        name: "",
        parent: "",
        project: "",
        remark: "",
      };
      this.$emit("initModuleTable");
      this.$emit("formatModuleTreeData");
      this.$refs["moduleForm"].resetFields();
    },
    handleEditModuleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          updateModule(this.moduleForm)
            .then((res) => {
              let status = res.status;
              let data = res.data;
              if (status === 200) {
                if (data.hasOwnProperty("status") && data.status == "fail") {
                  this.$Message.error(data.msg);
                } else {
                  this.$Message.info("修改成功");
                  this.handleEditModuleCancel();
                }
              } else {
                this.$Message.error("修改失败");
              }
            })
            .catch((err) => {
              this.$Modal.error({
                title: "系统异常，请联系管理员……",
                content: JSON.stringify(err),
                closable: true,
              });
            });
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
